﻿/*
Copyright (c) 2010 Neil Wood

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package envisage.Controls
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.GradientType;
	import flash.display.InterpolationMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.utils.getDefinitionByName;
	
	public class Desktop extends Control
	{
		public function Desktop()
		{
			super();
			
			this.addEventListener(Event.ADDED_TO_STAGE, OnAddedToStage);
		}
		
		private function OnAddedToStage(e:Event):void
		{
			this.stage.addEventListener(Event.RESIZE, OnStageResize);
			RequireLayout();
		}
		
		private function OnStageResize(e:Event):void
		{
			RequireLayout();
		}
		
		private var col:Object;
		public function StyleFlatCol(col:uint):void
		{
			this.col = col;
			RequireLayout();
		}
		
		public static var overlay:Sprite = new Sprite();
		
		private var base:DisplayObject;
		public function set Base(value:DisplayObject):void { this.base = value; RequireLayout(); }
		public function get Base():DisplayObject { return base; }
		
		private static const TILE:int = 0;
		private static const STRETCH_TOP_LEFT:int = 1;
		private static const STRETCH_CENTER:int = 2;
		
		private var options:int;
		private var bd:BitmapData;
		private var bm:Bitmap;
		private function StyleBitmap(from:Object):void
		{
			if( from is BitmapData ) bd = BitmapData(from);
			else
			{
				var c:Class = Class(getDefinitionByName(from as String));
				bd = new c();
			}
		}
		public function StyleBitmapTile(from:Object):void
		{
			StyleBitmap(from);
			options = TILE;
			RequireLayout();
		}
		
		public function StyleBitmapStretchTopLeft(from:Object):void
		{
			StyleBitmap(from);
			options = STRETCH_TOP_LEFT;
			RequireLayout();
		}
		
		public function StyleBitmapStretchCenter(from:Object):void
		{
			StyleBitmap(from);
			options = STRETCH_CENTER;
			RequireLayout();
		}
		
		private var gradCols:Array;
		private var gradAlphas:Array;
		private var gradRatios:Array;
		private var gradRads:Number;
		private var gradSpread:String;
		private var gradLinRGB:Boolean;
		private var gradFocal:Number = NaN;
		public function StyleLinearGradient(cols:Array, alphas:Array, ratios:Array, rads:Number, spread:String = "pad", linearRGB:Boolean = false):void
		{
			gradCols = cols;
			gradAlphas = alphas;
			gradRatios = ratios;
			gradRads = rads;
			gradSpread = spread;
			gradLinRGB = linearRGB;
		}
		
		public function StyleRadialGradient(cols:Array, alphas:Array, ratios:Array, rads:Number, focalRatio:Number = 0, spread:String = "pad", linearRGB:Boolean = false):void
		{
			gradCols = cols;
			gradAlphas = alphas;
			gradRatios = ratios;
			gradRads = rads;
			gradSpread = spread;
			gradLinRGB = linearRGB;
			gradFocal = focalRatio;
		}
		
		private var minWidth:Number = NaN;
		public function set MinWidth(value:Number):void { minWidth = value; RequireLayout(); }
		
		private var minHeight:Number = NaN;
		public function set MinHeight(value:Number):void { minHeight = value; RequireLayout(); }
		
		
		private static var t0Matrix:Matrix = new Matrix();
		protected override function Layout():void
		{
			var sw:Number = stage == null? parent == null? width  : parent.width  : stage.stageWidth;
			var sh:Number = stage == null? parent == null? height : parent.height : stage.stageHeight;
			
			//trace(sw, sh);
			
			if( ! isNaN(minWidth) && sw < minWidth ) sw = minWidth;
			if( ! isNaN(minHeight)&& sh < minHeight) sh = minHeight;
			
			
			if( col != null )
			{
				graphics.clear();
				graphics.beginFill(col as uint, 1);
				graphics.drawRect(0, 0, sw, sh);
				graphics.endFill();
			}
			
			else if (bd != null )
			{
				if( options == STRETCH_TOP_LEFT || options == STRETCH_CENTER)
				{
					if( bm == null ) { bm = new Bitmap(bd); addChild(bm); }
					
					bm.width = sw;
					bm.scaleY = bm.scaleX;
					if( bm.height < sh ) 
					{
						bm.height = sh;
						bm.scaleX =bm.scaleY;
					}
					
					if(options == STRETCH_CENTER )
					{
						bm.x = sw/2 - bm.width/2;
						bm.y = sh/2 - bm.height/2;
					}
				}
				
				else if( options == TILE )
				{
					graphics.clear();
					graphics.beginBitmapFill(bd, null, true, false);
					graphics.drawRect(0, 0, sw, sh);
					graphics.endFill();
				}
			}
			
			else if ( gradCols != null )
			{
				t0Matrix.identity();
				t0Matrix.createGradientBox(sw, sh, gradRads, 0, 0);
				
				graphics.clear();
				graphics.beginGradientFill(isNaN(gradFocal)? GradientType.LINEAR : GradientType.RADIAL,
											gradCols, gradAlphas, gradRatios, t0Matrix,
												gradSpread, gradLinRGB? InterpolationMethod.LINEAR_RGB : InterpolationMethod.RGB, gradFocal); 
				graphics.drawRect(0, 0, sw, sh);
				graphics.endFill();
			}
			
			if( base != null )
			{
				if( scale )
				{
					base.width = sw;
					base.height = sh;
				}
				
				else
				{
					base.x = sw/2 - base.width/2;
					base.y = sh/2 - base.height/2;
				}
			}
			
			if( stage != null )
			{
				stage.addChild(overlay);
			}
		}
		
		private var scale:Boolean = true;
		public function set Scale(value:Boolean):void { scale = value; RequireLayout(); }
	}
}
